//
//  HomeNaoBaoFormTableViewCell.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/1.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "HomeNaoBaoFormTableViewCell.h"

@interface HomeNaoBaoFormTableViewCell ()

@end

@implementation HomeNaoBaoFormTableViewCell

// 重写表格位置，使每一行cell都分隔开
//- (void)setFrame:(CGRect)frame{
//    //    frame.origin.x += 10;
//    frame.origin.y += 0;
//    frame.size.height -= 5;
//    //    frame.size.width -= 20;
//    [super setFrame:frame];
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.contentView addSubview:self.userHeadView];
        [self.userHeadView addSubview:self.iconTouxiang]; // 头像阴影
        [self.contentView addSubview:self.nameText]; // 名称
        [self.contentView addSubview:self.guanzhuBut]; // 关注
        [self.contentView addSubview:self.iconXunzhang]; // 等级勋章
        [self.contentView addSubview:self.textDengji]; // 等级
        [self.contentView addSubview:self.iconGuankan]; // 浏览人数图片
        [self.contentView addSubview:self.numLable];  // 浏览人数
        [self.contentView addSubview:self.contentText]; // 标题
        [self.contentView addSubview:self.iconShijian];  //  时间图标
        [self.contentView addSubview:self.shiJianText];  //  发布时间
        [self.shadowView  addSubview:self.shangYeText];
        [self.contentView addSubview:self.shadowView]; //  内容简介
        [self.contentView addSubview:self.imageFabiao];  // 用户发表图片
        [self.contentView addSubview:self.zuijiaText];  //  最佳评论标签
        [self.contentView addSubview:self.thumpUp];
        [self.contentView addSubview:self.thumpUpNum];
        [self.imageFabiao addSubview:self.labellingLab];
        [self.imageFabiao addSubview:self.labellingLab1];
        [self.imageFabiao addSubview:self.labellingLab2];
        
        [self.contentView addSubview:self.backBgColor];
        
//        [self showNaoBaoCell];
    }
    return self;
}

- (void)setModel:(HomeStormFromModel_Two *)model
{
    [_iconTouxiang sd_setBackgroundImageWithURL:[NSURL URLWithString:model.head_img] forState:0 placeholderImage:[UIImage imageNamed:@""]];
    
    _nameText.text = model.nickname;
    _textDengji.text = [NSString stringWithFormat:@"%@级",[model.level stringValue]];
    
    BOOL result = [self StringIsNullOrEmpty:[model.viewnum stringValue]];
    
    if (result) {
        _numLable.text = @"0";
    }else{
        _numLable.text = [model.viewnum stringValue];
    }
    
    NSString *timeCurrent = [self timeWithTimeIntervalString:[model.create_date stringValue]];
    _shiJianText.text = timeCurrent;
    
    _contentText.text = model.title;
    
    self.labellingLab .hidden = NO;
    self.labellingLab1.hidden = NO;
    self.labellingLab2.hidden = NO;
    if (model.industryid.count == 3) {
        self.labellingLab.text = model.industryid[0];
        self.labellingLab1.text = model.industryid[1];
        self.labellingLab2.text = model.industryid[2];
    }else if (model.industryid.count == 2){
        self.labellingLab.text = model.industryid[0];
        self.labellingLab1.text = model.industryid[1];
        self.labellingLab2.hidden = YES;
    }else{
        self.labellingLab.text = model.industryid[0];
        self.labellingLab1.hidden = YES;
        self.labellingLab2.hidden = YES;
    }
    
    _shangYeText.text = [NSString stringWithFormat:@"风暴简介：%@",model.info];
    [KQlable changeLineSpaceForLabel:_shangYeText WithSpace:3.f];
    
    [_shiJianText updateLayout];
    [_numLable updateLayout];
    [_thumpUpNum updateLayout];
    
    [self showNaoBaoCell:model];
}
// 若后台返回值为NULL或者<NULL>时，返回yes
- (BOOL)StringIsNullOrEmpty:(NSString *)str
{
    return (str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0);
}
// 时间戳转换
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSTimeInterval interval    =[timeString doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"]; // yyyy-MM-dd HH:mm:ss
    NSString *dateString       = [formatter stringFromDate: date];
    
    return dateString;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)showNaoBaoCell:(HomeStormFromModel_Two *)model
{
    _userHeadView.sd_layout
    .topSpaceToView(self.contentView, kHeightScale(12))
    .leftSpaceToView(self.contentView, kWidthScale(15))
    .widthIs(kWidthScale(25))
    .heightIs(kHeightScale(25));
    
    _iconTouxiang.sd_layout
    .topSpaceToView(self.userHeadView,kHeightScale(2))
    .leftSpaceToView(self.userHeadView,kWidthScale(2))
    .rightSpaceToView(self.userHeadView,kWidthScale(2))
    .bottomSpaceToView(self.userHeadView,kHeightScale(2));
    
    _nameText.sd_layout
    .topSpaceToView(self.contentView,kHeightScale(10))
    .leftSpaceToView(self.userHeadView,kWidthScale(5))
    .widthIs(kWidthScale(150))
    .autoHeightRatio(0);
    
    _guanzhuBut.sd_layout
    .topSpaceToView(self.nameText,kHeightScale(-12.5))
    .rightSpaceToView(self.contentView,kWidthScale(15))
    .widthIs(kWidthScale(44))
    .heightIs(kHeightScale(25));
    
    _iconXunzhang.sd_layout
    .topSpaceToView(self.nameText,kHeightScale(3))
    .leftEqualToView(self.nameText)
    .widthIs(kWidthScale(11))
    .heightIs(kHeightScale(11));
    
    _textDengji.sd_layout
    .centerYEqualToView(self.iconXunzhang)
    .leftSpaceToView(self.iconXunzhang,kWidthScale(3))
    .widthIs(kWidthScale(30))
    .autoHeightRatio(0);
    
    _contentText.sd_layout
    .topSpaceToView(self.iconXunzhang,kHeightScale(10))
    .leftEqualToView(self.userHeadView)
    .rightEqualToView(self.guanzhuBut)
    .autoHeightRatio(0);
    
    _iconShijian.sd_layout
    .topSpaceToView(self.contentText,kHeightScale(5))
    .leftEqualToView(self.userHeadView)
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(13));
    
    _shiJianText.sd_layout
    .centerYEqualToView(self.iconShijian)
    .leftSpaceToView(self.iconShijian,kWidthScale(5))
    .autoHeightRatio(0);
    [_shiJianText setSingleLineAutoResizeWithMaxWidth:(40)];
    
    _iconGuankan.sd_layout
    .centerYEqualToView(self.iconShijian)
    .leftSpaceToView(self.shiJianText,kWidthScale(10))
    .widthIs(kWidthScale(13))
    .heightIs(kHeightScale(8.6));
    
    _numLable.sd_layout
    .centerYEqualToView(self.iconGuankan)
    .leftSpaceToView(self.iconGuankan,kWidthScale(5))
    .autoHeightRatio(0);
    [_numLable setSingleLineAutoResizeWithMaxWidth:(40)];
    
    _thumpUp.sd_layout
    .centerYEqualToView(self.iconShijian)
    .leftSpaceToView(self.numLable,kWidthScale(10))
    .widthIs(kWidthScale(12))
    .heightIs(kHeightScale(12));
    
    _thumpUpNum.sd_layout
    .centerYEqualToView(self.thumpUp)
    .leftSpaceToView(self.thumpUp,kWidthScale(5))
    .autoHeightRatio(0);
    [_thumpUpNum setSingleLineAutoResizeWithMaxWidth:(40)];
    
    _shadowView.sd_layout
    .topSpaceToView(self.shiJianText,kHeightScale(5))
    .leftEqualToView(self.iconShijian)
    .rightEqualToView(self.guanzhuBut)
    .heightIs(kHeightScale(49));
    
    _shangYeText.sd_layout
    .topSpaceToView(self.shadowView, 0)
    .leftSpaceToView(self.shadowView, 0)
    .rightSpaceToView(self.shadowView, 0)
    .heightIs(kHeightScale(49));
    
    _imageFabiao.sd_layout
    .topSpaceToView(self.shadowView,kHeightScale(10))
    .leftEqualToView(self.shadowView)
    .rightEqualToView(self.shadowView)
    .heightIs(kHeightScale(110));
    
    _labellingLab.sd_layout
    .topSpaceToView(self.imageFabiao,kHeightScale(5))
    .leftSpaceToView(self.imageFabiao,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    
    _labellingLab1.sd_layout
    .topSpaceToView(self.imageFabiao,kHeightScale(5))
    .leftSpaceToView(self.labellingLab,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    
    _labellingLab2.sd_layout
    .topSpaceToView(self.imageFabiao,kHeightScale(5))
    .leftSpaceToView(self.labellingLab1,kWidthScale(5))
    .widthIs(kWidthScale(35))
    .heightIs(kHeightScale(15));
    
    
    
//    for (int i = 0; i < model.industryid.count; i++) {
//        _labellingLab = [[UILabel alloc] initWithFrame:CGRectMake(kWidthScale(5) + kWidthScale(40) * i, kHeightScale(5), kWidthScale(35), kHeightScale(15))];
//        
//        _labellingLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
//        _labellingLab.text = model.industryid[i];
//        _labellingLab.textColor = [UIColor colorWithWhite:0.9 alpha:1];
//        _labellingLab.font = [UIFont systemFontOfSize:kWidthScale(9)];
//        _labellingLab.textAlignment = NSTextAlignmentCenter;
//        _labellingLab.layer.cornerRadius = kWidthScale(5);
//        _labellingLab.layer.masksToBounds = YES;
//        [self.labelView addSubview:self.labellingLab];
//    }
    
    for (int i = 0; i < model.bestuser.count; i++) {
        _bestHeadImage = [[UIImageView alloc] initWithFrame:CGRectMake(kWidthScale(84) * (i+1) - (66*i), kHeightScale(308), kWidthScale(23), kHeightScale(23))];
        if (model.bestuser.count) {
            [_bestHeadImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[_model.bestuser[i] objectForKey:@"head_img"]]]];
        }
        [self.contentView addSubview:self.bestHeadImage]; //  最佳评论用户头像
    }
    [self.contentView addSubview:self.addTX];   // ➕
    
    if (model.bestuser.count) {
        _addTX.sd_layout
        .topSpaceToView(self.imageFabiao,kHeightScale(10))
        .leftSpaceToView(self.bestHeadImage,kWidthScale(-8))
        .widthIs(kWidthScale(23))
        .heightIs(kHeightScale(23));
    }else{
        _addTX.sd_layout
        .topSpaceToView(self.imageFabiao,kHeightScale(10))
        .leftSpaceToView(self.zuijiaText,kWidthScale(15))
        .widthIs(kWidthScale(23))
        .heightIs(kHeightScale(23));
    }
    
    _zuijiaText.sd_layout
    .centerYEqualToView(self.addTX)
    .leftEqualToView(self.imageFabiao)
    .widthIs(kWidthScale(50))
    .autoHeightRatio(0);
    
    _backBgColor.sd_layout
    .bottomEqualToView(self.contentView)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(kHeightScale(5));
}
// 头像
- (UIButton *)iconTouxiang
{
    if (!_iconTouxiang)
    {
        _iconTouxiang = [UIButton buttonWithType:UIButtonTypeCustom];
        _iconTouxiang.layer.cornerRadius = kWidthScale(23/2);
        _iconTouxiang.layer.masksToBounds = YES;
    }
    return _iconTouxiang;
}
// 名字
- (UILabel *)nameText
{
    if (!_nameText)
    {
        _nameText = [[UILabel alloc] init];
        _nameText.text = @"后悔的小公主";
        _nameText.font = [UIFont systemFontOfSize:kWidthScale(12)];
        _nameText.textColor = [UIColor colorWithHexString:@"#333333"];
    }
    return _nameText;
}
// 关注
- (UIButton *)guanzhuBut
{
    if (!_guanzhuBut)
    {
        _guanzhuBut = [UIButton buttonWithType:UIButtonTypeCustom];
        [_guanzhuBut setBackgroundImage:[UIImage imageNamed:@"guanzhu"] forState:0];
        _guanzhuBut.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _guanzhuBut;
}
// 等级勋章
- (UIImageView *)iconXunzhang
{
    if (!_iconXunzhang)
    {
        _iconXunzhang = [[UIImageView alloc] init];
        _iconXunzhang.image=[UIImage imageNamed:@"dengjis"];
    }
    return _iconXunzhang;
}
// 等级
- (UILabel *)textDengji
{
    if (!_textDengji)
    {
        _textDengji = [[UILabel alloc] init];
        _textDengji.text = @"1级";
        _textDengji.textColor=[UIColor colorWithHexString:@"#999999"];
        _textDengji.font=[UIFont systemFontOfSize:kWidthScale(10)];
    }
    return _textDengji;
}
// 观看人数图片
- (UIImageView *)iconGuankan
{
    if (!_iconGuankan)
    {
        _iconGuankan = [[UIImageView alloc] init];
        _iconGuankan.image=[UIImage imageNamed:@"liulans"];
    }
    return _iconGuankan;
}
// 观看人数
- (UILabel *)numLable
{
    if (!_numLable)
    {
        _numLable = [[UILabel alloc] init];
        _numLable.text=@"13";
        _numLable.textColor = [UIColor colorWithHexString:@"#999999"];
        _numLable.font=[UIFont systemFontOfSize:kWidthScale(12)];
    }
    return _numLable;
}
// 标题
- (UILabel *)contentText
{
    if (!_contentText)
    {
        _contentText = [[UILabel alloc] init];
        _contentText.textColor = [UIColor colorWithHexString:@"#333333"];
        _contentText.text=@"什么时候觉得，数据分析真的非常有用";
        _contentText.font = [UIFont systemFontOfSize:kWidthScale(14)];
    }
    return _contentText;
}
// 时间图标
- (UIImageView *)iconShijian
{
    if (!_iconShijian)
    {
        _iconShijian = [[UIImageView alloc] init];
        _iconShijian.image = [UIImage imageNamed:@"time"];
    }
    return _iconShijian;
}
// 具体发布时间
- (UILabel *)shiJianText
{
    if (!_shiJianText)
    {
        _shiJianText = [[UILabel alloc] init];
        _shiJianText.text = @"8.15";
        _shiJianText.textColor = [UIColor colorWithHexString:@"#999999"];
        _shiJianText.font = [UIFont systemFontOfSize:kWidthScale(12)];
        _shiJianText.textAlignment = NSTextAlignmentLeft;
    }
    return _shiJianText;
}
// 内容简介
- (KQlable *)shangYeText
{
    if (!_shangYeText)
    {
        _shangYeText = [[KQlable alloc] init];
        _shangYeText.text = @"风暴简介：个环境可凉快和包教包零零落落零零落落零零落落零零落落零零落落啦啦啦";
        _shangYeText.numberOfLines = 0;
//        _shangYeText.backgroundColor = [UIColor colorWithHexString:@"#fbfbfb"];
        _shangYeText.backgroundColor = [UIColor clearColor];
        _shangYeText.textColor = [UIColor colorWithHexString:@"#666666"];
        _shangYeText.font = [UIFont systemFontOfSize:kWidthScale(11)];
        _shangYeText.layer.cornerRadius = kWidthScale(12);
        _shangYeText.layer.masksToBounds = YES;
        [KQlable changeLineSpaceForLabel:_shangYeText WithSpace:5.0f];
        
    }
    return _shangYeText;
}
//- (void)drawTextInRect:(CGRect)rect {
//    UIEdgeInsets insets = {0, 5, 0, 5};
//    [self drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
//}

// 用户发表图片
- (UIImageView *)imageFabiao
{
    if (!_imageFabiao)
    {
        _imageFabiao = [[UIImageView alloc] init];
        _imageFabiao.image=[UIImage imageNamed:@"ditu"];
        _imageFabiao.layer.cornerRadius = kWidthScale(12);
        _imageFabiao.layer.masksToBounds = YES;
    }
    return _imageFabiao;
}

- (UILabel *)labellingLab
{
    if (!_labellingLab)
    {
        _labellingLab = [[UILabel alloc] init];
        _labellingLab.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _labellingLab.textColor = [UIColor colorWithWhite:1 alpha:1];
        _labellingLab.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _labellingLab.textAlignment = NSTextAlignmentCenter;
        _labellingLab.layer.cornerRadius = kWidthScale(5);
        _labellingLab.layer.masksToBounds = YES;
    }
    return _labellingLab;
}

- (UILabel *)labellingLab1
{
    if (!_labellingLab1)
    {
        _labellingLab1 = [[UILabel alloc] init];
        _labellingLab1.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _labellingLab1.textColor = [UIColor colorWithWhite:1 alpha:1];
        _labellingLab1.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _labellingLab1.textAlignment = NSTextAlignmentCenter;
        _labellingLab1.layer.cornerRadius = kWidthScale(5);
        _labellingLab1.layer.masksToBounds = YES;
    }
    return _labellingLab1;
}

- (UILabel *)labellingLab2
{
    if (!_labellingLab2)
    {
        _labellingLab2 = [[UILabel alloc] init];
        _labellingLab2.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _labellingLab2.textColor = [UIColor colorWithWhite:1 alpha:1];
        _labellingLab2.font = [UIFont systemFontOfSize:kWidthScale(9)];
        _labellingLab2.textAlignment = NSTextAlignmentCenter;
        _labellingLab2.layer.cornerRadius = kWidthScale(5);
        _labellingLab2.layer.masksToBounds = YES;
    }
    return _labellingLab2;
}

// 最佳评论
- (UILabel *)zuijiaText
{
    if (!_zuijiaText)
    {
        _zuijiaText = [[UILabel alloc] init];
        _zuijiaText.textColor = [UIColor colorWithHexString:@"#666666"];
        _zuijiaText.font = [UIFont systemFontOfSize:kWidthScale(11)];
        _zuijiaText.text = @"最佳讨论";
    }
    return _zuijiaText;
}

// ➕
- (UIButton *)addTX
{
    if (!_addTX)
    {
        _addTX = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addTX setBackgroundImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
    }
    return _addTX;
}

- (UIView *)shadowView
{
    if (!_shadowView)
    {
        _shadowView = [[UIView alloc] init];
//        _shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
//        _shadowView.layer.shadowOffset = CGSizeMake(0, 1);
//        _shadowView.layer.shadowOpacity = 0.2;
//        _shadowView.clipsToBounds = NO;
        
        _shadowView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"jianjie-bg"]];
        _shadowView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _shadowView;
}

- (UIImageView *)userHeadView
{
    if (!_userHeadView)
    {
        _userHeadView = [[UIImageView alloc] init];
        _userHeadView.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];;
        _userHeadView.layer.masksToBounds=YES;
        _userHeadView.layer.cornerRadius= kWidthScale(12.5);
        _userHeadView.layer.shadowColor = [UIColor blackColor].CGColor;
        _userHeadView.layer.shadowOffset = CGSizeMake(-3, 1);
        _userHeadView.layer.shadowOpacity = 0.4;
        _userHeadView.layer.shadowRadius = 2.0;
        _userHeadView.clipsToBounds = NO;
        
        _userHeadView.image = [UIImage imageNamed:@"touxiang"];
    }
    return _userHeadView;
}

- (UIButton *)thumpUp
{
    if (!_thumpUp)
    {
        _thumpUp = [UIButton buttonWithType:UIButtonTypeCustom];
        [_thumpUp setBackgroundImage:[UIImage imageNamed:@"zans"] forState:UIControlStateNormal];
    }
    return _thumpUp;
}

- (UILabel *)thumpUpNum
{
    if (!_thumpUpNum)
    {
        _thumpUpNum = [[UILabel alloc] init];
        _thumpUpNum.text = @"1111";
        _thumpUpNum.textColor = [UIColor colorWithHexString:@"#999999"];
        _thumpUpNum.font = [UIFont systemFontOfSize:kWidthScale(12)];
    }
    return _thumpUpNum;
}

- (UILabel *)backBgColor
{
    if (!_backBgColor)
    {
        _backBgColor = [[UILabel alloc] init];
        _backBgColor.backgroundColor = [UIColor colorWithHexString:@"#fbfbfb"];
    }
    return _backBgColor;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
